#!/usr/bin/env python3

import imp
import os
import shutil
import sys

import common
build_linux = imp.load_source('build-linux', os.path.join(kwargs['root_dir'], 'build_linux'))
run = imp.load_source('run', os.path.join(kwargs['root_dir'], 'run'))

parser = self.get_argparse(
    argparse_args={
        'description': '''Bisect the Linux kernel on gem5 boots.

More information at: https://github.com/cirosantilli/linux-kernel-module-cheat#bisection
'''},
    default_args={
        'gem5': True,
        'linux_build_id': 'bisect',
    },
)
args = self.setup(parser)
# We need a clean rebuild because rebuilds at different revisions:
# - may fail
# - may not actually rebuild all files, e.g. on header changes
self.rmrf(kwargs['linux_build_dir'])
build_linux.LinuxComponent().do_build(args)
status = run.main(args, {
    'eval': 'm5 exit',
})
if status == 125 or status == 127:
    status = 1
sys.exit(status)
